<?php defined('SYSPATH') OR die('No direct access allowed.');

/**
 *
 * @property  Category_Model $category
 *
 */
class Post_Controller extends Forum_Controller {

	  public function __construct() {
			parent::__construct();
			$this->template->set_global('basic_href', Router::uri('forum', array('controller'=>'post'))."/");
		}

		public function edit() {
			$this->history = FALSE;
			if (! $id = $this->input->post('id')) {
				message::add(Kohana::lang('post.edit.no_data'), 'error');
				Event::run('system.404');
			}
			if ($this->user===FALSE) {
				message::add(Kohana::lang('post.edit.must_login'), 'error');
        url::redirect(Router::uri('auth', array('method'=>'login')));
      }
			$post = ORM::factory('forum_post', $id);
			if (!$post->loaded) {
				message::add(Kohana::lang('post.edit.not_found'), 'error');
				Event::run('forum.error');
			}
			if (!$this->acl->allowed($post,'update')) {
				message::add(Kohana::lang('post.edit.no_access'), 'error');
				Event::run('system.403');
      }
			if ($data = $this->input->post() AND isset($data['text'])) {
				if ($post->validate($data)) {
					Event::run('forum.before.update.topic', $post);
					$post->save();
					Event::run('forum.after.update.topic', $post);
					message::add(Kohana::lang('post.edit.result_ok'), 'success');
					url::redirect($this->template->basic_href.$post->id);
				}
				else {
					$post->text = $data['text'];
					$post->title = $data['title'];
					message::add_validation($data->errors('post'));
					message::add($data, 'custom');
				}
			}
      $category = ORM::factory('forum_category', $post->category_id);
      if (!$category->loaded) {
				message::add(Kohana::lang('post.parent_category_not_found'), 'error');
				Event::run('forum.error');
			}
			$content = new View('forum/post_redactor');
			$content->action = 'edit';
			$content->category = $category;
			$content->post = $post;
			message::add(array('title' => $post->title), 'custom');
			$content->editor = (Editor::factory('post')->set_value($post->text)->render(FALSE, TRUE));
			$this->template->content = $content;
			$this->template->head['title'] = Kohana::lang('post.edit.title');
			$this->template->nav->breadcrumb = $category->parents;
		}

		public function add($id = NULL) {
			$this->history = FALSE;
			if ($this->user===FALSE) {
				message::add(Kohana::lang('post.add.must_login'), 'error');
        url::redirect(Router::uri('auth', array('method'=>'login')));
      }
      if (!$this->acl->allowed('post', 'create')) {
				message::add(Kohana::lang('post.add.no_access'), 'error');
				Event::run('system.403');
      }
			if (is_null($id) OR $id===FALSE) {
				message::add(Kohana::lang('post.parent_category_not_set'), 'error');
				Event::run('forum.error');
			}
			if ($data = $this->input->post()) {
				$post = ORM::factory('forum_post');
        $post->created = time();
        $post->category_id = $id;
        $post->author_id = $this->user->id;
				if ($post->validate($data)) {
					Event::run('forum.before.create.topic', $post);
					$post->save();
					Event::run('forum.after.create.topic', $post);
					message::add(Kohana::lang('post.add.result_ok'), 'success');
					url::redirect($this->template->basic_href.$post->id);
				}
				else {
					if (Captcha::instance()->invalid_count() > Kohana::config('cms.captcha_max_tries'))
						Event::run('cms.captcha.ban');
					message::add_validation($data->errors('post'));
					message::add($this->input->post(), 'custom');
				}
			}
      $category = ORM::factory('forum_category', $id);
      if (!$category->loaded) {
				message::add(Kohana::lang('post.parent_category_not_found'), 'error');
				Event::run('forum.error');
			}
			$content = new View('forum/post_redactor');
			$content->action = 'add';
			$content->category = $category;
			$content->editor = (Editor::factory('post')->set_value(message::custom('text'))->render(FALSE));
			$this->template->content = $content;
			$this->template->head['title'] = Kohana::lang('post.add.title');
			$this->template->nav->breadcrumb = $category->parents;
		}

		public function view($id = NULL) {
			if (is_null($id) OR $id===FALSE) {
				message::add(Kohana::lang('post.view.no_id'), 'error');
				Event::run('forum.error');
			}
			$post = ORM::factory('forum_post', $id);
			if (!$post->loaded) {
				message::add(Kohana::lang('post.view.not_found'), 'error');
				Event::run('forum.error');
			}
			$content = new View('forum/show_post');
			$content->post = $post;
			$page_num = $this->input->get('page', 1);
			$per_page = intval(Kohana::config('forum.comments_per_page'));
			$offset = ($page_num - 1)*$per_page;
			$content->comments = $post->limit($per_page, $offset)->forum_comments;
			$pages = Pagination::factory(array
			(
				'style' => Kohana::config('forum.pagination_style'),
				'items_per_page' => $per_page,
				'query_string' => 'page',
				'total_items' => $post->count_last_query()
			));
			$content->pages = $pages;
			/* TODO переделать через ACL */
			//if (access::can('forum_comment', 'create', $post)) {
			if ($this->acl->allowed('comment', 'create')) {
				$content->comments_enabled = TRUE;
				$content->editor = Editor::factory('comment')->set_value(message::custom('text'))->render(FALSE);
			}
			else $content->comments_enabled = FALSE;
			$this->template->content = $content;
			$this->template->head['title'] = text::limit_chars($post->title, 16, NULL, TRUE);
			$this->template->nav->breadcrumb = $post->category->get_parents(TRUE);
			Event::run('forum.show.post', $post);
		}

		public function __call($method, $args) {
			if (!ctype_digit($method)) {
        Event::run('system.404');
      }
			else $this->view($method);
		}
}